﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using PS.DBUtility;
using System.Collections.Generic;
namespace PS.DAL
{
    /// <summary>
    /// 数据访问类:ExamQuestionType
    /// </summary>
    public partial class ExamQuestionType
    {
        public ExamQuestionType()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录

        /// </summary>
        public bool Exists(int ExamQuestionTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ExamQuestionType");
            strSql.Append(" where ExamQuestionTypeID=@ExamQuestionTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamQuestionTypeID", SqlDbType.Int,4)
};
            parameters[0].Value = ExamQuestionTypeID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据

        /// </summary>
        public int Add(PS.Model.ExamQuestionType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ExamQuestionType(");
            strSql.Append("ExamSysParaID,QuestionType,QuestionCount,SingleQuestionScore,AutoCheckQuestion,AutoGo,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8)");
            strSql.Append(" values (");
            strSql.Append("@ExamSysParaID,@QuestionType,@QuestionCount,@SingleQuestionScore,@AutoCheckQuestion,@AutoGo,@Creator,@CreateTime,@Editor,@EditTime,@DispOrder,@ValidMark,@Spare1,@Spare2,@Spare3,@Spare4,@SpareDecimal5,@SpareDecimal6,@SpareDecimal7,@SpareDecimal8)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamSysParaID", SqlDbType.Int,4),
					new SqlParameter("@QuestionType", SqlDbType.Int,4),
					new SqlParameter("@QuestionCount", SqlDbType.Int,4),
					new SqlParameter("@SingleQuestionScore", SqlDbType.Decimal,9),
					new SqlParameter("@AutoCheckQuestion", SqlDbType.Bit,1),
					new SqlParameter("@AutoGo", SqlDbType.Int,4),
					new SqlParameter("@Creator", SqlDbType.VarChar,20),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.VarChar,20),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@SpareDecimal5", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal6", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal7", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal8", SqlDbType.Decimal,9)};
            parameters[0].Value = model.ExamSysParaID;
            parameters[1].Value = model.QuestionType;
            parameters[2].Value = model.QuestionCount;
            parameters[3].Value = model.SingleQuestionScore;
            parameters[4].Value = model.AutoCheckQuestion;
            parameters[5].Value = model.AutoGo;
            parameters[6].Value = model.Creator;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.Editor;
            parameters[9].Value = model.EditTime;
            parameters[10].Value = model.DispOrder;
            parameters[11].Value = model.ValidMark;
            parameters[12].Value = model.Spare1;
            parameters[13].Value = model.Spare2;
            parameters[14].Value = model.Spare3;
            parameters[15].Value = model.Spare4;
            parameters[16].Value = model.SpareDecimal5;
            parameters[17].Value = model.SpareDecimal6;
            parameters[18].Value = model.SpareDecimal7;
            parameters[19].Value = model.SpareDecimal8;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据

        /// </summary>
        public bool Update(PS.Model.ExamQuestionType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ExamQuestionType set ");
            strSql.Append("ExamSysParaID=@ExamSysParaID,");
            strSql.Append("QuestionType=@QuestionType,");
            strSql.Append("QuestionCount=@QuestionCount,");
            strSql.Append("SingleQuestionScore=@SingleQuestionScore,");
            strSql.Append("AutoCheckQuestion=@AutoCheckQuestion,");
            strSql.Append("AutoGo=@AutoGo,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Editor=@Editor,");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("DispOrder=@DispOrder,");
            strSql.Append("ValidMark=@ValidMark,");
            strSql.Append("Spare1=@Spare1,");
            strSql.Append("Spare2=@Spare2,");
            strSql.Append("Spare3=@Spare3,");
            strSql.Append("Spare4=@Spare4,");
            strSql.Append("SpareDecimal5=@SpareDecimal5,");
            strSql.Append("SpareDecimal6=@SpareDecimal6,");
            strSql.Append("SpareDecimal7=@SpareDecimal7,");
            strSql.Append("SpareDecimal8=@SpareDecimal8");
            strSql.Append(" where ExamQuestionTypeID=@ExamQuestionTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamSysParaID", SqlDbType.Int,4),
					new SqlParameter("@QuestionType", SqlDbType.Int,4),
					new SqlParameter("@QuestionCount", SqlDbType.Int,4),
					new SqlParameter("@SingleQuestionScore", SqlDbType.Decimal,9),
					new SqlParameter("@AutoCheckQuestion", SqlDbType.Bit,1),
					new SqlParameter("@AutoGo", SqlDbType.Int,4),
					new SqlParameter("@Creator", SqlDbType.VarChar,20),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.VarChar,20),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@SpareDecimal5", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal6", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal7", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal8", SqlDbType.Decimal,9),
					new SqlParameter("@ExamQuestionTypeID", SqlDbType.Int,4)};
            parameters[0].Value = model.ExamSysParaID;
            parameters[1].Value = model.QuestionType;
            parameters[2].Value = model.QuestionCount;
            parameters[3].Value = model.SingleQuestionScore;
            parameters[4].Value = model.AutoCheckQuestion;
            parameters[5].Value = model.AutoGo;
            parameters[6].Value = model.Creator;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.Editor;
            parameters[9].Value = model.EditTime;
            parameters[10].Value = model.DispOrder;
            parameters[11].Value = model.ValidMark;
            parameters[12].Value = model.Spare1;
            parameters[13].Value = model.Spare2;
            parameters[14].Value = model.Spare3;
            parameters[15].Value = model.Spare4;
            parameters[16].Value = model.SpareDecimal5;
            parameters[17].Value = model.SpareDecimal6;
            parameters[18].Value = model.SpareDecimal7;
            parameters[19].Value = model.SpareDecimal8;
            parameters[20].Value = model.ExamQuestionTypeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据

        /// </summary>
        public bool Delete(int ExamQuestionTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ExamQuestionType ");
            strSql.Append(" where ExamQuestionTypeID=@ExamQuestionTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamQuestionTypeID", SqlDbType.Int,4)
};
            parameters[0].Value = ExamQuestionTypeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据

        /// </summary>
        public bool DeleteList(string ExamQuestionTypeIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ExamQuestionType ");
            strSql.Append(" where ExamQuestionTypeID in (" + ExamQuestionTypeIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体

        /// </summary>
        public PS.Model.ExamQuestionType GetModel(int ExamQuestionTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ExamQuestionTypeID,ExamSysParaID,QuestionType,QuestionCount,SingleQuestionScore,AutoCheckQuestion,AutoGo,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8 from ExamQuestionType ");
            strSql.Append(" where ExamQuestionTypeID=@ExamQuestionTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamQuestionTypeID", SqlDbType.Int,4)
};
            parameters[0].Value = ExamQuestionTypeID;

            PS.Model.ExamQuestionType model = new PS.Model.ExamQuestionType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return GetModelByDataSet(ds);

        }
        /// <summary>
        /// 得到一个对象实体 
        /// </summary>
        public PS.Model.ExamQuestionType GetModelByDataSet(DataSet ds)
        {

            if (ds.Tables[0].Rows.Count > 0)
            {
                return GetModelByDataRow(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }

        }

        public PS.Model.ExamQuestionType GetModelByDataRow(DataRow dr)
        {
            PS.Model.ExamQuestionType model = new PS.Model.ExamQuestionType();
            if (dr["ExamQuestionTypeID"].ToString() != "")
            {
                model.ExamQuestionTypeID = int.Parse(dr["ExamQuestionTypeID"].ToString());
            }
            if (dr["ExamSysParaID"].ToString() != "")
            {
                model.ExamSysParaID = int.Parse(dr["ExamSysParaID"].ToString());
            }
            if (dr["QuestionType"].ToString() != "")
            {
                model.QuestionType = int.Parse(dr["QuestionType"].ToString());
            }
            if (dr["QuestionCount"].ToString() != "")
            {
                model.QuestionCount = int.Parse(dr["QuestionCount"].ToString());
            }
            if (dr["SingleQuestionScore"].ToString() != "")
            {
                model.SingleQuestionScore = decimal.Parse(dr["SingleQuestionScore"].ToString());
            }
            if (dr["AutoCheckQuestion"].ToString() != "")
            {
                if ((dr["AutoCheckQuestion"].ToString() == "1") || (dr["AutoCheckQuestion"].ToString().ToLower() == "true"))
                {
                    model.AutoCheckQuestion = true;
                }
                else
                {
                    model.AutoCheckQuestion = false;
                }
            }
            if (dr["AutoGo"].ToString() != "")
            {
                model.AutoGo = int.Parse(dr["AutoGo"].ToString());
            }
            model.Creator = dr["Creator"].ToString();
            if (dr["CreateTime"].ToString() != "")
            {
                model.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
            }
            model.Editor = dr["Editor"].ToString();
            if (dr["EditTime"].ToString() != "")
            {
                model.EditTime = DateTime.Parse(dr["EditTime"].ToString());
            }
            if (dr["DispOrder"].ToString() != "")
            {
                model.DispOrder = int.Parse(dr["DispOrder"].ToString());
            }
            if (dr["ValidMark"].ToString() != "")
            {
                model.ValidMark = int.Parse(dr["ValidMark"].ToString());
            }
            model.Spare1 = dr["Spare1"].ToString();
            model.Spare2 = dr["Spare2"].ToString();
            model.Spare3 = dr["Spare3"].ToString();
            model.Spare4 = dr["Spare4"].ToString();
            if (dr["SpareDecimal5"].ToString() != "")
            {
                model.SpareDecimal5 = decimal.Parse(dr["SpareDecimal5"].ToString());
            }
            if (dr["SpareDecimal6"].ToString() != "")
            {
                model.SpareDecimal6 = decimal.Parse(dr["SpareDecimal6"].ToString());
            }
            if (dr["SpareDecimal7"].ToString() != "")
            {
                model.SpareDecimal7 = decimal.Parse(dr["SpareDecimal7"].ToString());
            }
            if (dr["SpareDecimal8"].ToString() != "")
            {
                model.SpareDecimal8 = decimal.Parse(dr["SpareDecimal8"].ToString());
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ExamQuestionTypeID,ExamSysParaID,QuestionType,QuestionCount,SingleQuestionScore,AutoCheckQuestion,AutoGo,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8 ");
            strSql.Append(" FROM ExamQuestionType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetListBySqlString(string sqlString)
        {
            return DbHelperSQL.Query(sqlString);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PS.Model.ExamQuestionType> GetListModel(string strWhere)
        {
            List<PS.Model.ExamQuestionType> results = new List<PS.Model.ExamQuestionType>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ExamQuestionType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                   results.Add( GetModelByDataRow(dr));
                } 
            }
            else
            {
                return null;
            }
            return results; 
        }


        /// <summary>
        /// 获得前几行数据

        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ExamQuestionTypeID,ExamSysParaID,QuestionType,QuestionCount,SingleQuestionScore,AutoCheckQuestion,AutoGo,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8 ");
            strSql.Append(" FROM ExamQuestionType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public PS.Model.ExamQuestionType GetExamQuestionTypeByQuestionType(List<PS.Model.ExamQuestionType> list, int QuestionTypeID)
        {
            foreach (PS.Model.ExamQuestionType examQuestionType in list)
            {
                if (examQuestionType.QuestionType == QuestionTypeID)
                    return examQuestionType;
            }
            return null;
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ExamQuestionType";
            parameters[1].Value = "ExamQuestionTypeID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

